package com.udaos.modules.sales.services;

import java.util.List;

import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

import com.udaos.common.base.Service;
import com.udaos.models.customer.Ud_customer;
import com.udaos.models.customer.Ud_sales;
import com.udaos.modules.customer.services.UdCustomerService;

@IocBean(args = {"refer:dao"})
public class UdSalesService extends Service<Ud_sales>{
	
	@Inject
	private UdCustomerService customerService;
	public UdSalesService(Dao dao) {
		super(dao);
	}

	/**
	 * 奖励 
	 * @param reference_code
	 * @param companyId 
	 */
	@Aop(TransAop.READ_COMMITTED)
	public void reward(String reference_code, String companyId) {
		if(Strings.isBlank(reference_code)){
			return ;
		}else{
			//判断是否有效
			Cnd cnd=Cnd.NEW();
			cnd.and("sale_code", "=", reference_code);
			List<Ud_customer> list=customerService.query(cnd);
			if(null !=list && list.size()>0){
				Ud_customer customer=list.get(0);
				Cnd cnd1=Cnd.NEW();
				cnd1.and("customer_id", "=", customer.getId());
				cnd1.and("company_id", "=", companyId);
				cnd1.and("state", "=", 1);
				List<Ud_sales> sList=this.query(cnd1);
				if(null != sList && sList.size()>0){
					Ud_sales sales=sList.get(0);
					sales.setMoney(sales.getMoney()+100);
					this.updateIgnoreNull(sales);
				}
			}
		}
	}
}
